﻿package com.cadre.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cadre.entity.Cadre;
import com.cadre.entity.Matter;

import com.cadre.entity.User;
import com.cadre.service.MatterServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



@Controller
public class MatterController {
	   @Autowired
		private MatterServiceImpl msi;
		
		//查找所有事项
		@RequestMapping("/selectAllMatter")
		public String  selectAllMatter(Matter matter,@RequestParam(required=true,defaultValue="1")int pageNum,@RequestParam(required=true,defaultValue="5")int pageSize,Model model,HttpServletRequest request){
		PageHelper.startPage(pageNum, pageSize);
		List<Matter> list=this.msi.selectAllMatter();
		PageInfo<Matter> page=new PageInfo<Matter>(list);
		model.addAttribute("matter2",list);
		model.addAttribute("page", page);
		return "/showMatter2.jsp";
		}
		
		//添加事项
		@RequestMapping("/addMatter")
		public String  addMatter(Matter matter,Model model){
		System.out.println("11111");
		boolean result=this.msi.addMatter(matter);
		if(result){
			model.addAttribute("matter2",result);
			return "/new.jsp";
		}else{
			return "/Error.jsp";
		}
		}		
		
		//删除事项Cadre
				@RequestMapping("/CadredeleteByid")
				public String  CadreDeleteByid(Integer id){
			
				boolean result=this.msi.deleteByid(id);
				
					return "forward:selectAllMatter";
				}
				
		
		
		//删除事项（admin）
		@RequestMapping("/deleteByid")
		public String  deleteByid(Integer id){
	
		boolean result=this.msi.deleteByid(id);
		System.out.println("删除成功");
		if(result){
			return "forward:selectAllMatter1";
		}
			return "/Error.jsp";
		}
		
		//更新前，需要先查找
		@RequestMapping("/toUpdateMatter")
		public String selectMatterById(Integer id, Model model){	
			  Matter matter = this.msi.selectMatter(id);
				model.addAttribute("matter", matter);
				return "/matter.jsp";
			}
		//更新了
		@RequestMapping("/doUpdateMatter")
		public String doUpdateProduct(Matter matter,Model model){	
			  boolean result = this.msi.updateMatter(matter);
			if(result){
				return "forward:selectAllMatter1";
			}else{
				System.out.println("修改失败");
			}
			return "/matter.jsp";
			}
		
		
		
		//查找所有事项（admin）
				@RequestMapping("/selectAllMatter1")
				public String  selectAllMatter1(Matter matter,Model model,@RequestParam(required=true,defaultValue="1")int pageNum,@RequestParam(required=true,defaultValue="5")int pageSize,HttpServletRequest request){
				System.out.println("11111");
				PageHelper.startPage(pageNum, pageSize);
				List<Matter> list=this.msi.selectAllMatter();
				PageInfo<Matter> page=new PageInfo<Matter>(list);
				model.addAttribute("matter3",list);
				model.addAttribute("page", page);
				return "/showMatter.jsp";
				}
				
		//村民查看事项（user）
				@RequestMapping("/selectAllMatter3")
				public String  selectAllMatter3(Matter matter,Model model,@RequestParam(required=true,defaultValue="1")int pageNum,@RequestParam(required=true,defaultValue="5")int pageSize,HttpServletRequest request){
				PageHelper.startPage(pageNum, pageSize);
				List<Matter> list=this.msi.selectAllMatter();
				PageInfo<Matter> page=new PageInfo<Matter>(list);
				model.addAttribute("matter4",list);
				model.addAttribute("page", page);
				return "/showMatter3.jsp";
				}
				
				
		
}
